#include "phoneloginwidget.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRegularExpression>

#include "model/datacenter.h"
#include "toast.h"
#include "mainwidget.h"

using namespace model;


PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
    : QWidget{parent}
{
    // 1. 设置窗口本身属性
    this->setFixedSize(318, 447);
    this->setWindowTitle("VQ");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(0);
    layout->setVerticalSpacing(5);
    layout->setContentsMargins(30, 42, 30, 0);
    this->setLayout(layout);


    // 3. 创建标题
    QPushButton* titleBtn = new QPushButton();
    titleBtn->setFixedSize(90, 90);
    titleBtn->setIconSize(QSize(90, 90));
    titleBtn->setIcon(QIcon(":/resource/image/Icon_logo.png"));
    titleBtn->setStyleSheet("QPushButton { border:none; }");

    // 4. 创建手机号输入框
    QString editStyle = "QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); "
                        "font-size: 15px; padding-left: 5px; }";
    phoneEdit = new CustomLineEdit();
    phoneEdit->setFixedHeight(40);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    phoneEdit->setPlaceholderText("输入手机号码");
    phoneEdit->setStyleSheet(editStyle);

    // 5. 创建短信验证码输入框
    verifyCodeEdit = new CustomLineEdit();
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    verifyCodeEdit->setPlaceholderText("短信验证码");
    verifyCodeEdit->setStyleSheet(editStyle);

    // 6. 创建发送验证码按钮
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; "
                            "background-color: transparent; color: rgb(45, 119, 229); }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    sendVerifyCodeBtn = new QPushButton();
    sendVerifyCodeBtn->setText("发送验证码");
    sendVerifyCodeBtn->setFixedSize(100, 40);
    sendVerifyCodeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    sendVerifyCodeBtn->setStyleSheet(btnWhiteStyle);

    // 7. 创建提交按钮
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; "
                            "background-color: rgb(7, 193, 96); color: rgb(255, 255, 255);}";
    btnGreenStyle += "QPushButton:hover { background-color: rgb(56, 205, 127); }"
                     "QPushButton:pressed { background-color: rgb(6, 173, 86); }";
    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    submitBtn->setStyleSheet(btnGreenStyle);

    // 8. 创建 切换到用户名 模式按钮
    userModeBtn = new QPushButton();
    userModeBtn->setText("切换到用户名");
    userModeBtn->setFixedSize(100, 40);
    userModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    userModeBtn->setStyleSheet(btnWhiteStyle);

    // 9. 创建 登录注册 模式
    switchModeBtn = new QPushButton();
    switchModeBtn->setText("注册");
    switchModeBtn->setFixedSize(100, 40);
    switchModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    switchModeBtn->setStyleSheet(btnWhiteStyle);

    // 10. 添加到布局管理器
    layout->addWidget(titleBtn, 0, 1, 1, 1);
    layout->addWidget(phoneEdit, 1, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 2, 0, 1, 4);
    layout->addWidget(sendVerifyCodeBtn, 2, 4, 1, 1);
    layout->addWidget(submitBtn, 3, 0, 1, 5);
    layout->addWidget(userModeBtn, 4, 0, 1, 1);
    layout->addWidget(switchModeBtn, 4, 4, 1, 1);

    // 11. 添加信号槽
    connect(switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);

    connect(userModeBtn, &QPushButton::clicked, this, [=](){
        LoginWidget* loginWidget = new LoginWidget(nullptr);
        loginWidget->show();
        this->close();
    });

    connect(sendVerifyCodeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::sendVerifyCode);

    timer = new QTimer();
    connect(timer, &QTimer::timeout, this, &PhoneLoginWidget::countDown);

    connect(submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);
}

void PhoneLoginWidget::sendVerifyCode()
{
    // 1. 获取手机号码
    const QString& phone = this->phoneEdit->text();
    if (phone.isEmpty())
        return;

    QRegularExpression regex("^1[3-9]\\d{9}$");
    QRegularExpressionMatch match = regex.match(phone);
    if (match.hasMatch()) {
        this->currentPhone = phone;
    } else {
        Toast::showMessage("手机号码无效!");
        return;
    }

    // 2. 发送网络请求 获取验证码
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getVerifyCodeDone, this, &PhoneLoginWidget::sendVerifyCodeDone,
            Qt::UniqueConnection);
    dataCenter->getVerifyCodeAsync(phone);

    // 3. 开启定时器
    timer->start(1000);
}

void PhoneLoginWidget::sendVerifyCodeDone()
{
    Toast::showMessage("验证码已经发送");
}

void PhoneLoginWidget::clickSubmitBtn()
{
    // 1. 不能从输入框中拿到必要的内容
    const QString& phone = this->currentPhone;
    const QString& verifyCode = verifyCodeEdit->text();
    if (phone.isEmpty() || verifyCode.isEmpty())
    {
        Toast::showMessage("电话/验证码为空!");
        return;
    }

    // 2. 发送请求
    DataCenter* dataCenter = DataCenter::getInstance();
    if (isLoginMode)
    {
        // 登录
        connect(dataCenter, &DataCenter::phoneLoginDone, this, &PhoneLoginWidget::phoneLoginDone,
                Qt::UniqueConnection);
        dataCenter->phoneLoginAsync(phone, verifyCode);
    }
    else
    {
        // 注册
        connect(dataCenter, &DataCenter::phoneRegisterDone, this, &PhoneLoginWidget::phoneRegisterDone,
                Qt::UniqueConnection);
        dataCenter->phoneRegisterAsync(phone, verifyCode);
    }
}

void PhoneLoginWidget::phoneLoginDone(bool ok, const QString& reason)
{
    if (!ok)
    {
        Toast::showMessage("登录失败!" + reason);
        return;
    }
    Toast::showMessage("登录成功!");
    // 跳转到主窗口
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();

    // 关闭自己
    this->close();

}

void PhoneLoginWidget::phoneRegisterDone(bool ok, const QString &reason)
{
    if (!ok)
    {
        Toast::showMessage("注册失败!!" + reason);
        return;
    }
    Toast::showMessage("注册成功!");
    // 跳转到主窗口
    switchMode();

    // 清空输入框
    verifyCodeEdit->clear();

    // 倒计时按钮
    leftTime = 1;
}

void PhoneLoginWidget::countDown()
{
    if (leftTime <= 1)
    {
        sendVerifyCodeBtn->setEnabled(true);
        sendVerifyCodeBtn->setText("发送验证码");
        timer->stop();
        leftTime = 60;
        return;
    }

    leftTime -= 1;
    sendVerifyCodeBtn->setText(QString::number(leftTime) + " s");
    if (sendVerifyCodeBtn->isEnabled())
        sendVerifyCodeBtn->setEnabled(false);
}

void PhoneLoginWidget::switchMode()
{
    if (isLoginMode)
    {
        this->setWindowTitle("注册");
        submitBtn->setText("注册");
        switchModeBtn->setText("登录");
    }
    else
    {
        this->setWindowTitle("登录");
        submitBtn->setText("登录");
        switchModeBtn->setText("注册");
    }
    isLoginMode = !isLoginMode;
}













